/*
 * UserValueDisplay.java
 *
 * Created on 03 January 2004, 07:47
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.node.user;

import com.velocityme.valueobjects.NodeValue;
import com.velocityme.valueobjects.PersonValue;
import com.velocityme.valueobjects.PersonValueToString;
import com.velocityme.valueobjects.UserValue;
import java.util.Collection;
import java.util.Iterator;

/**
 *
 * @author  Robert Crida Work
 */
public class UserValueDisplay extends javax.swing.JPanel {
    
    private UserValue m_userValue;
    
    /** Creates new form UserValueDisplay */
    public UserValueDisplay(UserValue p_userValue, Collection p_allPersonValues) {
        initComponents();
        setUserValue(p_userValue, p_allPersonValues);
    }
    
    public void setUserValue(UserValue p_userValue, Collection p_allPersonValues) {
        NodeValue nodeValue = p_userValue.getNodeValue();
        if (p_userValue.userNameHasBeenSet())
            jTextFieldUserName.setText(p_userValue.getUserName());
        if (nodeValue != null && nodeValue.descriptionHasBeenSet())
            jTextAreaDescription.setText(nodeValue.getDescription());
        if (p_userValue.isEnabledHasBeenSet())
            jCheckBoxEnabled.setSelected(p_userValue.getIsEnabled().booleanValue());
        jComboBoxPerson.removeAllItems();
        if (p_allPersonValues != null) {
            Iterator i = p_allPersonValues.iterator();
            while(i.hasNext()) {
                jComboBoxPerson.addItem(new PersonValueToString((PersonValue)i.next()));
            }
        }
        else
            jComboBoxPerson.addItem(new PersonValueToString(p_userValue.getPersonValue()));
        if (p_userValue.getPersonValue() != null)
            jComboBoxPerson.setSelectedItem(new PersonValueToString(p_userValue.getPersonValue()));
        m_userValue = p_userValue;
    }
    
    public UserValue getUserValue() {
        if (m_userValue.getNodeValue() == null)
            m_userValue.setNodeValue(new NodeValue());
        m_userValue.setUserName(jTextFieldUserName.getText());
        m_userValue.setIsEnabled(new Boolean(jCheckBoxEnabled.isSelected()));
        m_userValue.setPersonValue((PersonValue)jComboBoxPerson.getSelectedItem());
        m_userValue.getNodeValue().setDescription(jTextAreaDescription.getText());
        return m_userValue;
    }
    
    public String getPassword() throws IllegalArgumentException {
        String password = jPasswordFieldPassword.getText();
        if (password.equals(jPasswordFieldRetypePassword.getText()))
            return password;
        else
            throw new IllegalArgumentException();
    }
    
    public void setEditable(boolean p_editable) {
        jTextFieldUserName.setEditable(p_editable);
        jTextAreaDescription.setEditable(p_editable);
        jLabelEnabled.setVisible(!p_editable);
        jCheckBoxEnabled.setVisible(!p_editable);
        jComboBoxPerson.setEnabled(p_editable);
    }
    
    public void setCreating(boolean p_creating) {
        jLabelPassword.setVisible(p_creating);
        jPasswordFieldPassword.setVisible(p_creating);
        jLabelRetypePassword.setVisible(p_creating);
        jPasswordFieldRetypePassword.setVisible(p_creating);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jLabelIcon = new javax.swing.JLabel();
        jLabelUserName = new javax.swing.JLabel();
        jTextFieldUserName = new javax.swing.JTextField();
        jLabelPassword = new javax.swing.JLabel();
        jPasswordFieldPassword = new javax.swing.JPasswordField();
        jLabelRetypePassword = new javax.swing.JLabel();
        jPasswordFieldRetypePassword = new javax.swing.JPasswordField();
        jLabelDescription = new javax.swing.JLabel();
        jScrollPaneDescription = new javax.swing.JScrollPane();
        jTextAreaDescription = new javax.swing.JTextArea();
        jLabelEnabled = new javax.swing.JLabel();
        jCheckBoxEnabled = new javax.swing.JCheckBox();
        jLabelPerson = new javax.swing.JLabel();
        jComboBoxPerson = new javax.swing.JComboBox();

        setLayout(new java.awt.GridBagLayout());

        jLabelIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/user-32x32.png")));
        add(jLabelIcon, new java.awt.GridBagConstraints());

        jLabelUserName.setText("User Name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelUserName, gridBagConstraints);

        jTextFieldUserName.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.weightx = 1.0;
        add(jTextFieldUserName, gridBagConstraints);

        jLabelPassword.setText("Password:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelPassword, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jPasswordFieldPassword, gridBagConstraints);

        jLabelRetypePassword.setText("Retype Password:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelRetypePassword, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jPasswordFieldRetypePassword, gridBagConstraints);

        jLabelDescription.setText("Description:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelDescription, gridBagConstraints);

        jTextAreaDescription.setEditable(false);
        jTextAreaDescription.setLineWrap(true);
        jTextAreaDescription.setWrapStyleWord(true);
        jScrollPaneDescription.setViewportView(jTextAreaDescription);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.weighty = 1.0;
        add(jScrollPaneDescription, gridBagConstraints);

        jLabelEnabled.setText("Enabled:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelEnabled, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(jCheckBoxEnabled, gridBagConstraints);

        jLabelPerson.setText("Person:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelPerson, gridBagConstraints);

        jComboBoxPerson.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(jComboBoxPerson, gridBagConstraints);

    }//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jCheckBoxEnabled;
    private javax.swing.JComboBox jComboBoxPerson;
    private javax.swing.JLabel jLabelDescription;
    private javax.swing.JLabel jLabelEnabled;
    private javax.swing.JLabel jLabelIcon;
    private javax.swing.JLabel jLabelPassword;
    private javax.swing.JLabel jLabelPerson;
    private javax.swing.JLabel jLabelRetypePassword;
    private javax.swing.JLabel jLabelUserName;
    private javax.swing.JPasswordField jPasswordFieldPassword;
    private javax.swing.JPasswordField jPasswordFieldRetypePassword;
    private javax.swing.JScrollPane jScrollPaneDescription;
    private javax.swing.JTextArea jTextAreaDescription;
    private javax.swing.JTextField jTextFieldUserName;
    // End of variables declaration//GEN-END:variables
    
}
